package com.prime.tutorial.moviestore.view.logistics;

import com.prime.tutorial.moviestore.Constants;
import com.prime.tutorial.moviestore.domain.Device;
import com.prime.tutorial.moviestore.domain.Model;
import com.prime.tutorial.moviestore.service.logistics.LogisticsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.event.ActionListener;
import javax.faces.model.SelectItem;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * Users: me
 * Date: 1 бер 2010
 * Time: 6:36:15
 * @author iryna.masyuk
 */

@Component("addDevices")
@Scope("request")
public class AddDevices {

    private LogisticsService logisticsService;
    //the quantity of devices
    private int quantity;

    //used to generate a list of device models for select menu
    private List availableModels;
    //used to store the result of the select from the dropdown list
    private String modelSelected;

    @Autowired
    public AddDevices(LogisticsService logisticsService) {
        this.logisticsService = logisticsService;
        
        //generating dropdown list of device models
        availableModels = new ArrayList();
        List<Model> models = logisticsService.getDeviceModels();
        for(int i=0; i<models.size();i++){
            SelectItem option = new SelectItem(models.get(i).getType(), models.get(i).getType());
            availableModels.add(option);
        }
    }

    public String addDevices(ActionEvent actionEvent){
        //TODO pass devices in loop for upper layer
        Model m = logisticsService.findModel(modelSelected);
        for(int i=0; i<quantity; i++){
            //creating device
            Device d = new Device();
            d.setModel(m);
            d.setState(logisticsService.findState(Constants.STATE_ON_STORAGE));
            logisticsService.createDevice(d);
        }
        String text = m.getType();
 //       showMessage("Devices are added successfully");
         FacesContext context = FacesContext.getCurrentInstance();
        //TODO Find out how not to display the message twice.
         context.addMessage(null, new FacesMessage("Devices are added successfully: " + text));
    //     context.addMessage(null, new FacesMessage("Second Message", "Additional Info Here..."));
        return null;
    }


  /*  private void showMessage(String message){
       FacesMessage facesMessage = new FacesMessage(FacesMessage.SEVERITY_INFO, message, "");
       FacesContext.getCurrentInstance().addMessage(null, facesMessage);
    }
    */
    //--------------------------------------------getters&setters
    public List getAvailableModels() {
        return availableModels;
    }

    public void setAvailableModels(List availableModels) {
        this.availableModels = availableModels;
    }

    public int getQuantity() {
        return quantity;
    }

    public void setQuantity(int quantity) {
        this.quantity = quantity;
    }

    public String getModelSelected() {
        return modelSelected;
    }

    public void setModelSelected(String modelSelected) {
        this.modelSelected = modelSelected;
    }
}
